/*
 * Licensed to the Apache Software Foundation (ASF) under one or more
 * contributor license agreements.  See the NOTICE file distributed with
 * this work for additional information regarding copyright ownership.
 * The ASF licenses this file to You under the Apache License, Version 2.0
 * (the "License"); you may not use this file except in compliance with
 * the License.  You may obtain a copy of the License at
 *
 *      http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */
package org.apache.catalina.startup;

import org.apache.catalina.Globals;
import org.apache.catalina.security.SecurityClassLoad;
import org.apache.catalina.startup.ClassLoaderFactory.Repository;
import org.apache.catalina.startup.ClassLoaderFactory.RepositoryType;
import org.apache.juli.logging.Log;
import org.apache.juli.logging.LogFactory;

import java.io.File;
import java.io.IOException;
import java.lang.reflect.InvocationTargetException;
import java.lang.reflect.Method;
import java.net.MalformedURLException;
import java.net.URL;
import java.util.ArrayList;
import java.util.List;
import java.util.regex.Matcher;
import java.util.regex.Pattern;

/**
 * Bootstrap loader for Catalina.  This application constructs a class loader
 * for use in loading the Catalina internal classes (by accumulating all of the
 * JAR files found in the "server" directory under "catalina.home"), and
 * starts the regular execution of the container.  The purpose of this
 * roundabout approach is to keep the Catalina internal classes (and any
 * other classes they depend on, such as an XML parser) out of the system
 * class path and therefore not visible to application level classes.
 *
 * @author Craig R. McClanahan
 * @author Remy Maucherat
 */
public final class Bootstrap {

    private static final Log log = LogFactory.getLog( Bootstrap.class );

    /**
     * Daemon object used by main.
     */
    private static final Object daemonLock = new Object();
    private static volatile Bootstrap daemon = null;

    //Tomcat 实例路径
    private static final File catalinaBaseFile;
    //Tomcat安装路径
    private static final File catalinaHomeFile;

    private static final Pattern PATH_PATTERN = Pattern.compile( "(\"[^\"]*\")|(([^,])*)" );

    /**
     * static 代码块首先执行
     */
    static {
        // Will always be non-null
        String userDir = System.getProperty( "user.dir" );

        // Home first，获取tomcat安装目录
        String home = System.getProperty( Globals.CATALINA_HOME_PROP );
        File homeFile = null;

        if (home != null) {
            File f = new File( home );
            try {
                homeFile = f.getCanonicalFile();
            } catch (IOException ioe) {
                homeFile = f.getAbsoluteFile();
            }
        }

        if (homeFile == null) {
            // First fall-back. See if current directory is a bin directory
            // in a normal Tomcat install
            File bootstrapJar = new File( userDir, "bootstrap.jar" );

            if (bootstrapJar.exists()) {
                File f = new File( userDir, ".." );
                try {
                    homeFile = f.getCanonicalFile();
                } catch (IOException ioe) {
                    homeFile = f.getAbsoluteFile();
                }
            }
        }

        if (homeFile == null) {
            // Second fall-back. Use current directory
            File f = new File( userDir );
            try {
                homeFile = f.getCanonicalFile();
            } catch (IOException ioe) {
                homeFile = f.getAbsoluteFile();
            }
        }

        catalinaHomeFile = homeFile;
        System.setProperty(
                Globals.CATALINA_HOME_PROP, catalinaHomeFile.getPath() );

        // Then base，Tomcat实例目录
        String base = System.getProperty( Globals.CATALINA_BASE_PROP );
        //如果tomcat实例目录没有设置，则与安装目录相同
        if (base == null) {
            catalinaBaseFile = catalinaHomeFile;
        } else {
            File baseFile = new File( base );
            try {
                baseFile = baseFile.getCanonicalFile();
            } catch (IOException ioe) {
                baseFile = baseFile.getAbsoluteFile();
            }
            catalinaBaseFile = baseFile;
        }
        System.setProperty(
                Globals.CATALINA_BASE_PROP, catalinaBaseFile.getPath() );
    }

    // -------------------------------------------------------------- Variables


    /**
     * Daemon reference.
     */
    private Object catalinaDaemon = null;

    //Tomcat 自定义了三个类加载器，先使用自己的类加载器，找不到才会使用JVM的双亲委派机制
    //common.loader Tomcat安装包下lib的jar，Tomcat实例下lib的jar
    ClassLoader commonLoader = null;
    //应用服务类加载器（父类加载器为common）
    ClassLoader catalinaLoader = null;
    //所有web应用的类加载器，加载webapps1、webapps2下的内容，也就是WEB-INF/classes,lib的内容，每个web应用隔离
    ClassLoader sharedLoader = null;


    // -------------------------------------------------------- Private Methods
    //初始化类加载器commonLoader、catalinaLoader、sharedLoader
    //tomcat给每个应用都配一个自己的ClassLoader
    private void initClassLoaders() {
        try {
            //common.loader Tomcat安装包下lib的jar，Tomcat实例下lib的jar
            commonLoader = createClassLoader( "common", null );
            if (commonLoader == null) {
                // no config file, default to this loader - we might be in a 'single' env.
                commonLoader = this.getClass().getClassLoader();
            }
            //Tomcat实例服务类加载器（父类加载器为common）
            catalinaLoader = createClassLoader( "server", commonLoader );
            //所有web应用的类加载器
            sharedLoader = createClassLoader( "shared", commonLoader );
        } catch (Throwable t) {
            handleThrowable( t );
            log.error( "Class loader creation threw exception", t );
            System.exit( 1 );
        }
    }


    private ClassLoader createClassLoader(String name, ClassLoader parent)
            throws Exception {

        String value = CatalinaProperties.getProperty( name + ".loader" );
        if ((value == null) || (value.equals( "" ))) {
            return parent;
        }

        value = replace( value );

        List<Repository> repositories = new ArrayList<>();

        String[] repositoryPaths = getPaths( value );

        for (String repository : repositoryPaths) {
            // Check for a JAR URL repository
            try {
                @SuppressWarnings("unused")
                URL url = new URL( repository );
                repositories.add( new Repository( repository, RepositoryType.URL ) );
                continue;
            } catch (MalformedURLException e) {
                // Ignore
            }

            // Local repository
            if (repository.endsWith( "*.jar" )) {
                repository = repository.substring
                        ( 0, repository.length() - "*.jar".length() );
                repositories.add( new Repository( repository, RepositoryType.GLOB ) );
            } else if (repository.endsWith( ".jar" )) {
                repositories.add( new Repository( repository, RepositoryType.JAR ) );
            } else {
                repositories.add( new Repository( repository, RepositoryType.DIR ) );
            }
        }

        return ClassLoaderFactory.createClassLoader( repositories, parent );
    }


    /**
     * System property replacement in the given string.
     *
     * @param str The original string
     * @return the modified string
     */
    protected String replace(String str) {
        // Implementation is copied from ClassLoaderLogManager.replace(),
        // but added special processing for catalina.home and catalina.base.
        String result = str;
        int pos_start = str.indexOf( "${" );
        if (pos_start >= 0) {
            StringBuilder builder = new StringBuilder();
            int pos_end = -1;
            while (pos_start >= 0) {
                builder.append( str, pos_end + 1, pos_start );
                pos_end = str.indexOf( '}', pos_start + 2 );
                if (pos_end < 0) {
                    pos_end = pos_start - 1;
                    break;
                }
                String propName = str.substring( pos_start + 2, pos_end );
                String replacement;
                if (propName.length() == 0) {
                    replacement = null;
                } else if (Globals.CATALINA_HOME_PROP.equals( propName )) {
                    replacement = getCatalinaHome();
                } else if (Globals.CATALINA_BASE_PROP.equals( propName )) {
                    replacement = getCatalinaBase();
                } else {
                    replacement = System.getProperty( propName );
                }
                if (replacement != null) {
                    builder.append( replacement );
                } else {
                    builder.append( str, pos_start, pos_end + 1 );
                }
                pos_start = str.indexOf( "${", pos_end + 1 );
            }
            builder.append( str, pos_end + 1, str.length() );
            result = builder.toString();
        }
        return result;
    }


    /**
     * Initialize catalinaDaemon实例
     *
     * @throws Exception Fatal initialization error
     */
    public void init() throws Exception {

        //初始化类加载器commonLoader、catalinaLoader、sharedLoader
        initClassLoaders();

        Thread.currentThread().setContextClassLoader( catalinaLoader );

        //使用catalinaLoader来加载Tomat的核心类
        SecurityClassLoad.securityClassLoad( catalinaLoader );

        // Load our startup class and call its process() method
        if (log.isDebugEnabled()) {
            log.debug( "Loading startup class" );
        }

        //反射方式加载并实例化Catalina
        Class<?> startupClass = catalinaLoader.loadClass( "org.apache.catalina.startup.Catalina" );
        Object startupInstance = startupClass.getConstructor().newInstance();

        // Set the shared extensions class loader
        if (log.isDebugEnabled()) {
            log.debug( "Setting startup class properties" );
        }


        //反射方式  设置Catalina的类加载器为sharedLoader
        String methodName = "setParentClassLoader";
        Class<?> paramTypes[] = new Class[1];
        paramTypes[0] = Class.forName( "java.lang.ClassLoader" );
        Object paramValues[] = new Object[1];
        paramValues[0] = sharedLoader;
        //tomcat初始化Catalina时,设置Catalina类所用加载器的父类加载器
        Method method =
                startupInstance.getClass().getMethod( methodName, paramTypes );
        method.invoke( startupInstance, paramValues );

        //引用Catalina实例
        catalinaDaemon = startupInstance;
    }

    /**
     * Load daemon.
     * 反射方式调用Catalina的load方法
     */
    private void load(String[] arguments) throws Exception {
        // Call the load() method
        String methodName = "load";
        Object param[];
        Class<?> paramTypes[];
        if (arguments == null || arguments.length == 0) {
            paramTypes = null;
            param = null;
        } else {
            paramTypes = new Class[1];
            paramTypes[0] = arguments.getClass();
            param = new Object[1];
            param[0] = arguments;
        }
        Method method =
                catalinaDaemon.getClass().getMethod( methodName, paramTypes );
        if (log.isDebugEnabled()) {
            log.debug( "Calling startup class " + method );
        }
        method.invoke( catalinaDaemon, param );
    }


    /**
     * getServer() for configtest
     */
    private Object getServer() throws Exception {
        String methodName = "getServer";
        Method method = catalinaDaemon.getClass().getMethod( methodName );
        return method.invoke( catalinaDaemon );
    }


    // ------------------------ Main Program
    /**
     * Load the Catalina daemon.
     *
     * @param arguments Initialization arguments
     * @throws Exception Fatal initialization error
     */
    public void init(String[] arguments) throws Exception {
        init();
        load( arguments );
    }


    /**
     * Start the Catalina daemon.
     *
     * @throws Exception Fatal start error
     */
    public void start() throws Exception {
        if (catalinaDaemon == null) {
            init();
        }

        Method method = catalinaDaemon.getClass().getMethod( "start", (Class[]) null );
        method.invoke( catalinaDaemon, (Object[]) null );
    }


    /**
     * Stop the Catalina Daemon.
     *
     * @throws Exception Fatal stop error
     */
    public void stop() throws Exception {
        Method method = catalinaDaemon.getClass().getMethod( "stop", (Class[]) null );
        method.invoke( catalinaDaemon, (Object[]) null );
    }


    /**
     * Stop the standalone server.
     *
     * @throws Exception Fatal stop error
     */
    public void stopServer() throws Exception {

        Method method =
                catalinaDaemon.getClass().getMethod( "stopServer", (Class[]) null );
        method.invoke( catalinaDaemon, (Object[]) null );
    }


    /**
     * Stop the standalone server.
     *
     * @param arguments Command line arguments
     * @throws Exception Fatal stop error
     */
    public void stopServer(String[] arguments) throws Exception {

        Object param[];
        Class<?> paramTypes[];
        if (arguments == null || arguments.length == 0) {
            paramTypes = null;
            param = null;
        } else {
            paramTypes = new Class[1];
            paramTypes[0] = arguments.getClass();
            param = new Object[1];
            param[0] = arguments;
        }
        Method method =
                catalinaDaemon.getClass().getMethod( "stopServer", paramTypes );
        method.invoke( catalinaDaemon, param );
    }


    /**
     * Set flag.让tomcat在关闭端口阻塞监听关闭命令
     *
     * @param await <code>true</code> if the daemon should block
     * @throws Exception Reflection error
     */
    public void setAwait(boolean await)
            throws Exception {

        Class<?> paramTypes[] = new Class[1];
        paramTypes[0] = Boolean.TYPE;
        Object paramValues[] = new Object[1];
        paramValues[0] = Boolean.valueOf( await );
        Method method =
                catalinaDaemon.getClass().getMethod( "setAwait", paramTypes );
        method.invoke( catalinaDaemon, paramValues );
    }

    public boolean getAwait() throws Exception {
        Class<?> paramTypes[] = new Class[0];
        Object paramValues[] = new Object[0];
        Method method =
                catalinaDaemon.getClass().getMethod( "getAwait", paramTypes );
        Boolean b = (Boolean) method.invoke( catalinaDaemon, paramValues );
        return b.booleanValue();
    }


    /**
     * Destroy the Catalina Daemon.
     */
    public void destroy() {

        // FIXME

    }


    /**
     * Main method and entry point when starting Tomcat via the provided
     * scripts.
     *
     * @param args Command line arguments to be processed
     */
    public static void main(String args[]) {

        //Tomcat启动时同步方式
        synchronized (daemonLock) {
            if (daemon == null) {
                // Don't set daemon until init() has completed
                Bootstrap bootstrap = new Bootstrap();
                try {
                    //完成类加载器的初始化，初始化catalinaDaemon
                    bootstrap.init();
                } catch (Throwable t) {
                    handleThrowable( t );
                    t.printStackTrace();
                    return;
                }
                //获取bootstrap引用
                daemon = bootstrap;
            } else {
                // When running as a service the call to stop will be on a new
                // thread so make sure the correct class loader is used to
                // prevent a range of class not found exceptions.
                Thread.currentThread().setContextClassLoader( daemon.catalinaLoader );
            }
        }

        //执行启动命令start
        try {
            String command = "start";
            if (args.length > 0) {
                command = args[args.length - 1];
            }
            //start stop  startd   stopd的区别
            if (command.equals( "startd" )) {
                args[args.length - 1] = "start";
                //实际上执行的是通过反射方式获取到的 org.apache.catalina.startup.Catalina
                daemon.load( args );
                daemon.start();
            } else if (command.equals( "stopd" )) {
                args[args.length - 1] = "stop";
                daemon.stop();
            } else if (command.equals( "start" )) {
                //让tomcat在关闭端口阻塞监听关闭命令
                daemon.setAwait( true );
                daemon.load( args );
                daemon.start();
                if (null == daemon.getServer()) {
                    System.exit( 1 );
                }
            } else if (command.equals( "stop" )) {
                daemon.stopServer( args );
            } else if (command.equals( "configtest" )) {
                daemon.load( args );
                if (null == daemon.getServer()) {
                    System.exit( 1 );
                }
                System.exit( 0 );
            } else {
                log.warn( "Bootstrap: command \"" + command + "\" does not exist." );
            }
        } catch (Throwable t) {
            // Unwrap the Exception for clearer error reporting
            if (t instanceof InvocationTargetException &&
                    t.getCause() != null) {
                t = t.getCause();
            }
            handleThrowable( t );
            t.printStackTrace();
            System.exit( 1 );
        }
    }


    /**
     * Obtain the name of configured home (binary) directory. Note that home and
     * base may be the same (and are by default).
     *
     * @return the catalina home
     */
    public static String getCatalinaHome() {
        return catalinaHomeFile.getPath();
    }


    /**
     * Obtain the name of the configured base (instance) directory. Note that
     * home and base may be the same (and are by default). If this is not set
     * the value returned by {@link #getCatalinaHome()} will be used.
     *
     * @return the catalina base
     */
    public static String getCatalinaBase() {
        return catalinaBaseFile.getPath();
    }


    /**
     * Obtain the configured home (binary) directory. Note that home and
     * base may be the same (and are by default).
     *
     * @return the catalina home as a file
     */
    public static File getCatalinaHomeFile() {
        return catalinaHomeFile;
    }


    /**
     * Obtain the configured base (instance) directory. Note that
     * home and base may be the same (and are by default). If this is not set
     * the value returned by {@link #getCatalinaHomeFile()} will be used.
     *
     * @return the catalina base as a file
     */
    public static File getCatalinaBaseFile() {
        return catalinaBaseFile;
    }


    // Copied from ExceptionUtils since that class is not visible during start
    private static void handleThrowable(Throwable t) {
        if (t instanceof ThreadDeath) {
            throw (ThreadDeath) t;
        }
        if (t instanceof VirtualMachineError) {
            throw (VirtualMachineError) t;
        }
        // All other instances of Throwable will be silently swallowed
    }


    // Protected for unit testing
    protected static String[] getPaths(String value) {

        List<String> result = new ArrayList<>();
        Matcher matcher = PATH_PATTERN.matcher( value );

        while (matcher.find()) {
            String path = value.substring( matcher.start(), matcher.end() );

            path = path.trim();
            if (path.length() == 0) {
                continue;
            }

            char first = path.charAt( 0 );
            char last = path.charAt( path.length() - 1 );

            if (first == '"' && last == '"' && path.length() > 1) {
                path = path.substring( 1, path.length() - 1 );
                path = path.trim();
                if (path.length() == 0) {
                    continue;
                }
            } else if (path.contains( "\"" )) {
                // Unbalanced quotes
                // Too early to use standard i18n support. The class path hasn't
                // been configured.
                throw new IllegalArgumentException(
                        "The double quote [\"] character can only be used to quote paths. It must " +
                                "not appear in a path. This loader path is not valid: [" + value + "]" );
            } else {
                // Not quoted - NO-OP
            }

            result.add( path );
        }
        return result.toArray( new String[0] );
    }
}
